package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.api.model.vo.SysDictVo;
import com.weixing.mall.provider.mapper.DictMapper;
import com.weixing.mall.provider.model.domain.Dict;
import com.weixing.mall.provider.model.dto.sys.SysDictDto;
import com.weixing.mall.provider.model.query.SysDictQuery;
import com.weixing.mall.provider.model.vo.sys.SysDictFormVo;
import com.weixing.mall.provider.service.IDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 数据字典表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    @Override
    public IPage<SysDictVo> selectOfPage(Page initMpPage, SysDictQuery sysDictQuery) {
        return baseMapper.selectOfPage(initMpPage,sysDictQuery);
    }

    @Override
    public void save(SysDictDto dictDto) {
        Dict sysDict = new Dict();
        BeanUtils.copyProperties(dictDto,sysDict);
        if (null == dictDto.getId()){
            if(dictDto.getIsEnable()!=null){
                sysDict.setIsEnable(dictDto.getIsEnable().getValue());
            }
            save(sysDict);
        }else{
            if(dictDto.getIsEnable()!=null){
                sysDict.setIsEnable(dictDto.getIsEnable().getValue());
            }
            sysDict.setUpdatedTime(new Date());
            updateById(sysDict);
        }
    }

    @Override
    public List<SysDictFormVo> selectDictFormVoList(SysDictQuery sysDictQuery) {
        return baseMapper.selectDictFormVoList(sysDictQuery);
    }

    @Override
    public List<SysDictVo> getDictByCode(String dictGroupCode) {
        return baseMapper.getDictByCode(dictGroupCode);
    }
}
